package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.itheima.service.BlogService;
import com.itheima.dao.FileMapper;
import com.itheima.pojo.BlogSort;
import com.itheima.service.BlogSortService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private BlogService blogService;

    /**
     * 根据作者获取相关的博客
     * @param author
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/searchBlogByAuthor")
    public String searchBlogByAuthor(@RequestParam("author") String author, @RequestParam("currentPage")Long currentPage,@RequestParam("pageSize") Long pageSize){
        PageInfo pageInfo = blogService.searchBlogByAuthor(author, currentPage, pageSize);
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        map.put("code","success");
        map1.put("records",pageInfo);
        map.put("data",map1);
        map.put("total",pageInfo.getTotal());
        map.put("size",pageSize);
        map.put("current",currentPage);
        map.put("orders",null);
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return JSON.toJSONString(map);
    }

    /**
     * 根据分类获取相关的博客
     * @param blogSortUid
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/searchBlogBySort")
    public String searchBlogBySort(@RequestParam("blogSortUid") String blogSortUid, @RequestParam("currentPage")Long currentPage,@RequestParam("pageSize") Long pageSize){
        PageInfo pageInfo = blogService.searchBlogBySort(blogSortUid, currentPage, pageSize);
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        map.put("code","success");
        map1.put("records",pageInfo);
        map.put("data",map1);
        map.put("total",pageInfo.getTotal());
        map.put("size",pageSize);
        map.put("current",currentPage);
        map.put("orders",null);
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return JSON.toJSONString(map);
    }

    /**
     * 根据标签获取相关的博客
     * @param tagUid
    */
    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private BlogSortService blogSortService;

    /**
     * 搜索Blog
     * @param
     * @param currentPage
     * @param pageSize
     * @return
     */

    @GetMapping("/searchBlogByTag")
    public String searchBlogByTag(@RequestParam("tagUid") String tagUid, @RequestParam("currentPage")Long currentPage,@RequestParam("pageSize") Long pageSize) {
        PageInfo pageInfo = blogService.searchBlogByTag(tagUid, currentPage, pageSize);
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        map.put("code", "success");
        map1.put("records", pageInfo);
        map.put("data", map1);
        map.put("total", pageInfo.getTotal());
        map.put("size", pageSize);
        map.put("current", currentPage);
        map.put("orders", null);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);
        return JSON.toJSONString(map);
    }

    /**
     * 根据关键字获取播客
     * @param keywords
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/sqlSearchBlog")
    public Map<String,Object> sqlSearchBlog(@RequestParam("keywords") String keywords,@RequestParam(name = "currentPage",defaultValue = "1",required = false) Long currentPage, @RequestParam(name = "pageSize",defaultValue = "10",required = false) Long pageSize){
        //获取查询数据
        PageInfo pageInfo = blogService.sqlSearchBlog(keywords, Math.toIntExact(currentPage), Math.toIntExact(pageSize));
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        //封装查询数据
        List list = pageInfo.getList();
        List<Map> blogList=new ArrayList<>();
        for (Object o : list) {
            Map map = JSON.parseObject(JSON.toJSONString(o), Map.class);
            //获取图片地址
            String fileUid = fileMapper.getPicUrlByUid((String) map.get("fileUid"));
            List<String> photoUrl=new ArrayList<>();
            photoUrl.add(fileUid);
            map.put("photoUrl",photoUrl);
            //获取sortName
            BlogSort blogSort = blogSortService.findBlogByUid((String) map.get("blogSortUid"));
            map.put("blogSortName",blogSort.getSortName());

            blogList.add(map);
        }
        //封装data
        Map<String,Object> data=new HashMap<>();
        data.put("blogList",blogList);
        data.put("total",pageInfo.getTotal());
        data.put("pageSize",pageInfo.getPageSize());
        data.put("currentPage",pageInfo.getPageNum());
        data.put("totalPage",pageInfo.getPages());
        //将将data放入返回map
        result.put("data",data);

        return result;

    }
}
